package com.tian.library.permission

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2020/12/14 12:25
 */
internal object PermissionSettingPage {

    /**
     * 获取应用详情界面意图
     */
    fun getApplicationDetailsIntent(context: Context): Intent {
        var intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.parse("package:" + context.packageName)
        return intent
    }


    /**
     * 根据传入的权限自动选择最合适的权限设置页
     */
    fun getSmartPermissionIntent(context: Context, permissions: MutableSet<String>): Intent {
        if (permissions.isEmpty()) {
            return getApplicationDetailsIntent(context)
        }

        // 如果失败的权限里面包含了特殊权限
        if (PermissionUtil.containsSpecialPermission(permissions)) {
            if (permissions.size == 1) {
                val permission = permissions.first()
                if (PermissionConstant.MANAGE_EXTERNAL_STORAGE == permission) {
                    return getStoragePermissionIntent(context)
                }

                if (PermissionConstant.REQUEST_INSTALL_PACKAGES == permission) {
                    return getInstallPermissionIntent(context)
                }

                if (PermissionConstant.SYSTEM_ALERT_WINDOW == permission) {
                    return getWindowPermissionIntent(context)
                }

                if (PermissionConstant.NOTIFICATION_SERVICE == permission) {
                    return getNotifyPermissionIntent(context)
                }

                if (PermissionConstant.WRITE_SETTINGS == permission) {
                    return getSettingPermissionIntent(context)
                }
                return getApplicationDetailsIntent(context)
            }

            if (permissions.size == 3) {
                if (permissions.contains(PermissionConstant.MANAGE_EXTERNAL_STORAGE) &&
                        permissions.contains(PermissionConstant.READ_EXTERNAL_STORAGE) &&
                        permissions.contains(PermissionConstant.WRITE_EXTERNAL_STORAGE)) {
                    if (PermissionUtil.isHigherAndroid11()) {
                        return getStoragePermissionIntent(context)
                    }

                    return getApplicationDetailsIntent(context)
                }
                return getApplicationDetailsIntent(context)
            }
        }
        return getApplicationDetailsIntent(context)
    }

    /**
     * 获取存储权限设置界面意图
     */
    fun getStoragePermissionIntent(context: Context): Intent {
        var intent: Intent? = null
        if (PermissionUtil.isHigherAndroid11()) {
            intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
            intent?.data = Uri.parse("package:" + context.packageName)
        }
        if (intent == null || !PermissionUtil.areActivityIntent(context, intent!!)) {
            intent = getApplicationDetailsIntent(context)
        }
        return intent!!
    }

    /**
     * 获取安装权限设置界面意图
     */
    fun getInstallPermissionIntent(context: Context): Intent {
        var intent: Intent? = null
        if (PermissionUtil.isHigherAndroid8()) {
            intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
            intent?.data = Uri.parse("package:" + context.packageName)
        }

        if (intent == null || !PermissionUtil.areActivityIntent(context, intent!!)) {
            intent = getApplicationDetailsIntent(context)
        }
        return intent!!
    }


    /**
     * 获取悬浮窗权限设置界面意图
     */
    fun getWindowPermissionIntent(context: Context): Intent {
        var intent: Intent? = null
        if (PermissionUtil.isHigherAndroid6()) {
            intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
            // 在 Android 11 上面不能加包名跳转，因为就算加了也没有效果
            // 还有人反馈在 Android 11 的 TV 模拟器上会出现崩溃的情况
            // https://developer.android.google.cn/reference/android/provider/Settings#ACTION_MANAGE_OVERLAY_PERMISSION
            if (!PermissionUtil.isHigherAndroid11()) {
                intent.setData(Uri.parse("package:" + context.packageName))
            }
        }

        if (intent == null || !PermissionUtil.areActivityIntent(context, intent!!)) {
            intent = getApplicationDetailsIntent(context)
        }
        return intent!!
    }

    /**
     * 获取通知栏权限设置界面意图
     */
    fun getNotifyPermissionIntent(context: Context): Intent {
        var intent: Intent? = null
        if (PermissionUtil.isHigherAndroid8()) {
            intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
            intent?.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
        }

        if (intent == null || !PermissionUtil.areActivityIntent(context, intent!!)) {
            intent = getApplicationDetailsIntent(context)
        }
        return intent!!
    }


    /**
     * 获取系统设置权限界面意图
     */
    fun getSettingPermissionIntent(context: Context): Intent {
        var intent: Intent? = null
        if (PermissionUtil.isHigherAndroid6()) {
            intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
            intent?.data = Uri.parse("package:${context.packageName}")
        }
        if (intent == null || !PermissionUtil.areActivityIntent(context, intent!!)) {
            intent = getApplicationDetailsIntent(context)
        }
        return intent!!
    }
}